package com.lujianfei.netease_android_mvvm_kotlin

import android.os.Bundle
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.ActionBar
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.lujianfei.netease_android_mvvm_kotlin.adapter.MainAdapter
import com.lujianfei.netease_android_mvvm_kotlin.base.BaseActivity
import com.lujianfei.netease_android_mvvm_kotlin.databinding.ActivityMainBinding
import com.lujianfei.netease_android_mvvm_kotlin.entity.DataBean
import com.lujianfei.netease_android_mvvm_kotlin.viewmodel.MainViewModel
import kotlinx.android.synthetic.main.activity_main.*
import org.koin.android.ext.android.inject

class MainActivity : BaseActivity() {

    private val mViewModel : MainViewModel by inject()
    private var binding : ActivityMainBinding?= null
    private var mActionBarView: View?= null
    private var mAdapter = MainAdapter(mViewModel)
    private var mItemCancel: View? = null
    private var mItemEdit: MenuItem? = null
    private var mItemSelectAll: MenuItem? = null
    private var mItemUnSelectAll: MenuItem? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this,R.layout.activity_main)
        mViewModel.uiState.observe(this, Observer { uiStateNonNull->
            uiStateNonNull.datalist?.let {
                update(it)
            }
            uiStateNonNull.title?.let {
                setMiddleTitle(it)
            }
            uiStateNonNull.updateSelectAll?.let {
                updateSelectAll(it)
            }
            uiStateNonNull.showToast?.let {
                showToast(it)
            }
            uiStateNonNull.positionToChange?.let {
                notifyItemRangeChanged(it,mAdapter.itemCount)
            }
        })
        binding?.viewModel = mViewModel
        recyclerview.layoutManager = LinearLayoutManager(this);
        setSupportActionBar(toolBar)
        initView()
        mViewModel.load()
    }

    private fun initView() {
        mActionBarView = LayoutInflater.from(this).inflate(R.layout.actionbar_layout, null)
        recyclerview.adapter = mAdapter
        mItemCancel = mActionBarView?.findViewById(R.id.menu_item_cancel)
        mItemCancel?.setOnClickListener {  setEditMode(false) }
        supportActionBar?.customView = mActionBarView
        supportActionBar?.displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM
        setMiddleTitle("Mvvm Kotlin Demo")
    }

     fun setMiddleTitle(title: String) {
        val textView = mActionBarView?.findViewById<TextView>(R.id.tvTitle)
        textView?.text = title
    }

    fun update(dataListBean: MutableList<DataBean>) {
        mAdapter.setData(dataListBean)
    }

     fun setEditMode(editMode: Boolean) {
         mAdapter.setEditMode(editMode)
        if (editMode) {
            mItemCancel?.visibility = View.VISIBLE
            mItemSelectAll?.isVisible = true
            mItemEdit?.isVisible = false
            setMiddleTitle(resources.getQuantityString(R.plurals.selected,0,0))
            binding?.showDelete = true
        } else {
            mItemCancel?.visibility = View.GONE
            mItemSelectAll?.isVisible = false
            mItemUnSelectAll?.isVisible = false
            mItemEdit?.isVisible = true
            setMiddleTitle(getString(R.string.main_title));
            binding?.showDelete = false
        }
    }

     fun setSelectAll(selectAll: Boolean) {
         mViewModel.setSelectAll(selectAll)
        updateSelectAll(selectAll)
    }

     fun updateSelectAll(selectAll: Boolean) {
        if (selectAll) {
            mItemSelectAll?.isVisible = false
            mItemUnSelectAll?.isVisible = true
        } else {
            mItemSelectAll?.isVisible = true
            mItemUnSelectAll?.isVisible = false
        }
    }

     fun isEditMode(): Boolean {
         mAdapter.run {
             return isEditMode()
         }
    }

     fun notifyItemRangeChanged(position: Int, count: Int) {
         mAdapter.notifyItemRangeChanged(position, count)
     }


     fun showToast(s: String) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show()
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.menu_item_edit -> {
                setEditMode(true)
            }
            R.id.menu_item_selectAll -> {
                setSelectAll(true)
            }
            R.id.menu_item_unselectAll -> {
                setSelectAll(false)
            }
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.more, menu)
        mItemEdit = menu.findItem(R.id.menu_item_edit)
        mItemSelectAll = menu.findItem(R.id.menu_item_selectAll)
        mItemUnSelectAll = menu.findItem(R.id.menu_item_unselectAll)
        mItemSelectAll?.isVisible = false
        mItemUnSelectAll?.isVisible = false
        return super.onCreateOptionsMenu(menu)
    }

    override fun onBackPressed() {
        if (isEditMode()) {
            setEditMode(false)
        } else {
            super.onBackPressed()
        }
    }
}
